package com.affectu.user.action;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.ui.component.html.HtmlDiv;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

import com.affectu.category.Category;
import com.affectu.dao.DAOBase;
import com.affectu.user.User;

/*
 * 2008-8-28
 * @author daniel nathan
 */
@Name("profileAction")
public class ProfileAction extends DAOBase {

	private User user;
	private HtmlDiv categoryDiv;

	@Begin(join = true)
	public void uploadListener(UploadEvent event) {
		try {
			UploadItem item = event.getUploadItem();
			String fileName = item.getFileName();
			fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);

			Random rand = new Random();
			String faceImage = rand.nextInt(9999) + fileName;// 防止同名文件产生
			// this.getUser().setFaceImage(faceImage);
			this.getSessionScope().put("faceImage", faceImage);
			// this.getSessionScope().put("attachName", this.attachName);//
			String filepath = this.getRealPath("/") + "upload/userFace/";
			File file = new File(filepath, faceImage);
			FileInputStream fis = new FileInputStream(item.getFile());
			FileOutputStream out = new FileOutputStream(file);
			int bytes = 0;
			byte[] bteFile = new byte[1024];
			while ((bytes = fis.read(bteFile)) != -1) {
				out.write(bteFile, 0, bytes);
			}
			out.flush();
			out.close();

			File fo = new File(filepath, "small_" + faceImage); // 将要转换出的小图文件
			int nw = 40;
			AffineTransform transform = new AffineTransform();
			BufferedImage bis = ImageIO.read(file);
			int w = bis.getWidth();
			int h = bis.getHeight();
			int nh = (nw * h) / w;
			double sx = (double) nw / w;
			double sy = (double) nh / h;
			transform.setToScale(sx, sy);
			System.out.println(w + " " + h);
			AffineTransformOp ato = new AffineTransformOp(transform, null);
			BufferedImage bid = new BufferedImage(nw, nh,
					BufferedImage.TYPE_3BYTE_BGR);
			ato.filter(bis, bid);
			ImageIO.write(bid, "jpeg", fo); //

			FacesMessages.instance().add("文件上传成功！" + filepath + faceImage);
		} catch (Exception ex) {
			FacesMessages.instance().add("文件上传失败！");
			this.logException(ex);
		}
	}

	public ProfileAction() {
		this.user = this.getCurrentUser();
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@End(beforeRedirect = true)
	public String submit(User user) {
		try {
			if (this.getSessionScope().get("faceImage") != null) {
				user.setFaceImage(this.getSessionScope().get("faceImage")
						.toString());
				em.merge(user);
				this.getSessionScope().remove("faceImage");
			} else {
				em.merge(user);
			}

		} catch (Exception ex) {
			this.logException(ex);
		}
		return "/passport/index.xhtml";
	}

	public String addCategoryToSpecialty(Category category) {
		if ("".equals(this.user.getSpecialty())) {
			this.user.setSpecialty(category.getName());
		} else {
			this.user.setSpecialty(this.user.getSpecialty() + ","
					+ category.getName());
		}
		return "";
	}

	public String showCategoryDiv() {
		this.getCategoryDiv().setRendered(true);
		return "";
	}

	public HtmlDiv getCategoryDiv() {
		return categoryDiv;
	}

	public void setCategoryDiv(HtmlDiv categoryDiv) {
		this.categoryDiv = categoryDiv;
	}

}
